package com.zkingsoft.actions.superaction;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.model.Btn;
import com.zkingsoft.model.Function;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BtnService;
import com.zkingsoft.services.FunctionService;

/**
 * @project MatrixCms
 * @package FunctionController
 * @class com.zkingsoft.actions.superaction.FunctionController
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年4月21日10:31
 * @description 功能管理
 */
@Controller
@RequestMapping(value = "/super/function")
public class FunctionController extends BaseController {

	private static final Logger log = Logger.getLogger(FunctionController.class);

	@Resource
	private FunctionService currentService;

	@Resource
	private BtnService btnService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Function function, PaginationVO pageVo) {
		log.info("按钮列表...");
		return showList(currentService, function, pageVo);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Function function) {
		//设置功能等级
		if (function.getParentId() == 0) {
			function.setGrade(1);
		} else {
			Function parent = currentService.findById(function.getParentId());
			function.setGrade(parent.getGrade() + 1);
		}
		if (function.getId() != null) {
			return modify(currentService, function, "功能");
		} else {
			// 设置默认启用
			function.setStatus(1);
			
			return add(currentService, function, "功能");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Function function;
		if (id != null) {
			function = currentService.findById(id);
			request.setAttribute("obj", function);
		}
		List<Btn> btnList = btnService.findByModel(null);
		request.setAttribute("btnList", btnList);
		return "super/function-form";
	}

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}

	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(Long id) {
		Function function = super.findById(currentService, id);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, Arrays.asList(function), 0, 0);
	}
	
	
}